package com.zz.biz.inventory.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.R;
import com.zz.biz.inventory.domain.SmdInventory;
import com.zz.dto.InventoryResponse;
import com.zz.biz.inventory.service.ISmdInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zz.biz.inventory.mapper.SmdInventoryMapper;

/**
 * 库存管理Service业务层处理
 * 
 * @author zz
 * @date 2025-06-25
 */
@Service
public class SmdInventoryServiceImpl implements ISmdInventoryService
{
    @Autowired
    private SmdInventoryMapper smdInventoryMapper;

    /**
     * 查询库存管理
     * 
     * @param id 库存管理主键
     * @return 库存管理
     */
    @Override
    public SmdInventory selectSmdInventoryById(Long id)
    {
        return smdInventoryMapper.selectSmdInventoryById(id);
    }

    /**
     * 查询库存管理列表
     * 
     * @param smdInventory 库存管理
     * @return 库存管理
     */
    @Override
    public List<SmdInventory> selectSmdInventoryList(SmdInventory smdInventory)
    {
        return smdInventoryMapper.selectSmdInventoryList(smdInventory);
    }

    /**
     * 新增库存管理
     * 
     * @param smdInventory 库存管理
     * @return 结果
     */
    @Override
    public int insertSmdInventory(SmdInventory smdInventory)
    {
        return smdInventoryMapper.insertSmdInventory(smdInventory);
    }

    /**
     * 修改库存管理
     * 
     * @param smdInventory 库存管理
     * @return 结果
     */
    @Override
    public int updateSmdInventory(SmdInventory smdInventory)
    {
        return smdInventoryMapper.updateSmdInventory(smdInventory);
    }

    /**
     * 批量删除库存管理
     * 
     * @param ids 需要删除的库存管理主键
     * @return 结果
     */
    @Override
    public int deleteSmdInventoryByIds(Long[] ids)
    {
        return smdInventoryMapper.deleteSmdInventoryByIds(ids);
    }

    /**
     * 删除库存管理信息
     * 
     * @param id 库存管理主键
     * @return 结果
     */
    @Override
    public int deleteSmdInventoryById(Long id)
    {
        return smdInventoryMapper.deleteSmdInventoryById(id);
    }

    @Override
    public R<List<InventoryResponse>> isInStock(List<Map<String,Object>> skuCode) {
        return R.ok(smdInventoryMapper.isInStock(skuCode.stream().map(m-> JSON.toJSONString(m)).collect(Collectors.toList()))
                .stream().map(inventory->InventoryResponse.builder()
                        .sku(inventory.getSku())
                        .quantity(inventory.getQuantity().intValue())
                        .isInStock(inventory.getQuantity() > 0 ? 1 : 0)
                        .build())
                .collect(Collectors.toList()));
    }
}
